import struct
from codecs import StreamReader
import os
from . import Importer

def read_float32(f):
    return round(struct.unpack('f', f.read(4))[0], 4)


def read_float16(f):
    return round(struct.unpack('f', f.read(2))[0], 4)


def read_int32(f):
    return int.from_bytes(f.read(4), 'little')


def read_int16(f):
    return int.from_bytes(f.read(2), 'little')

class PartObj():
    def __init__(self):
        super().__init__()
        self.name = ''
        self.vertices = []
        self.uvs = []
        self.indices = []
        self.uvindices = []
        self.normals = []
        self.normalindice = []
        self.child = []
        self.coord = []
        self.scale = []

        self.bbmax = []
        self.bbmin = []

def read_animation(filepath):
    f = open(filepath, 'rb')
    f.seek(0x14)
    while True:
        flag = f.read(6)
        if flag == b'\xAA\x47\x2A\x05\x4B\x22':
            id = read_int16(f)
            f.read(0x1A)
            unk1 = read_float32(f)
            unk2 = read_int32(f)
            if f.read(2) == b'\xAA\x27':
                idd = read_int16(f)
                unk3 = read_int32(f)
                unk_count = read_int32(f)
        elif flag == b'\xAA\x47\x67\x05\x0D\x25':
            id = read_int16(f)
            f.read(4)
            unk1 = read_int32(f)
            f.read(0x14)
            unk2 = read_float32(f)
            if f.read(2) == b'\xAA\x27':
                id = read_int16(f)
                

